/*
 * @lc app=leetcode.cn id=257 lang=cpp
 *
 * [257] 二叉树的所有路径
 */

// @lc code=start

// struct TreeNode
// {
//     int val;
//     TreeNode *left;
//     TreeNode *right;
//     TreeNode() : val(0), left(nullptr), right(nullptr) {}
//     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
//     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
// };

#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    void search(TreeNode *t, vector<string> &ans, string &s)
    {
        string tmp = s;
        if (t->left)
        {
            s = tmp + "->" + to_string(t->left->val);
            search(t->left, ans, s);
        }
        if (t->right)
        {
            s = tmp + "->" + to_string(t->right->val);
            search(t->right, ans, s);
        }
        if (!t->left && !t->right)
        {
            ans.push_back(tmp);
        }
        s = tmp;
        return;
    }

    vector<string> binaryTreePaths(TreeNode *root)
    {
        if (!root)
            return {};
        if (root && !root->left && !root->right)
            return {to_string(root->val)};

        std::string s = to_string(root->val);
        vector<string> ans;
        search(root, ans, s);
        return ans;
    }
};
// @lc code=end
